package com.info.zhiduoduo.admin.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.info.zhiduoduo.admin.model.formbean.memorandum.CompleteMemorandumFormBean;
import com.info.zhiduoduo.admin.model.formbean.memorandum.QueryMemorandumByConditionFormBean;
import com.info.zhiduoduo.admin.model.formbean.memorandum.SaveMemorandumFormBean;
import com.info.zhiduoduo.admin.model.formbean.memorandum.UpdateMemorandumFormBean;
import com.info.zhiduoduo.common.constants.SmartResult;
import com.info.zhiduoduo.common.constants.StatusMsg;
import com.info.zhiduoduo.common.formbean.BaseSimpleFormBean;
import com.info.zhiduoduo.common.utils.StringUtil;
import com.info.zhiduoduo.core.service.common.BaseService;
import com.info.zhiduoduo.repository.dto.memorandum.MemorandumListDto;
import com.info.zhiduoduo.repository.entity.memorandum.PlatformMemorandum;
import com.info.zhiduoduo.repository.mapper.memorandum.PlatformMemorandumMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class MemorandumService extends BaseService {

	@Resource
	PlatformMemorandumMapper platformMemorandumMapper;

	@Resource
	AccountService accountService;

	/**
	 * 新增备忘录
	 * @param formBean
	 * @return
	 */
	public SmartResult saveMemorandum(BaseSimpleFormBean<SaveMemorandumFormBean> formBean){
		if(null == formBean || null == formBean.getReqdata() || 0 == formBean.getUserid()){
			return emptyDataSmartResult();
		}
		SaveMemorandumFormBean memorandumFormBean = formBean.getReqdata();
		PlatformMemorandum memorandum = new PlatformMemorandum();
		BeanUtils.copyProperties(memorandumFormBean, memorandum);
		//获取当前登录者
		memorandum.setFounderId(formBean.getUserid());
		//新增
		if(!memorandum.insert()){
			return smartResult(StatusMsg.StatusMsg_199);
		}
		return smartResult();
	}

	/**
	 * 列表查询(可以根据条件)
	 * @param formBean
	 * @return
	 */
	public SmartResult queryMemorandumByCondition(BaseSimpleFormBean<QueryMemorandumByConditionFormBean> formBean){
		if(null == formBean || null == formBean.getReqdata()){
			return emptyDataSmartResult();
		}
		int type = formBean.getReqdata().getType();
		int customerType = formBean.getReqdata().getCustomerType();
		String keyWord = formBean.getReqdata().getKeyWord();
		Page<Object> page = PageHelper.startPage(formBean.getPage(), formBean.getRows());
		List<MemorandumListDto> memorandumListDtoList = platformMemorandumMapper.queryMemorandumByCondition(keyWord,type,customerType);
		return smartResult(memorandumListDtoList, page.getTotal(), page.getPages(), page.getPageNum());
	}

	/**
	 * 根据ID查询开票详情
	 * @param formBean
	 * @return
	 */
	public SmartResult getMemorandumById(BaseSimpleFormBean<Integer> formBean){
		if(null == formBean || null == formBean.getReqdata() || 0 == formBean.getReqdata()){
			return emptyDataSmartResult();
		}
		Integer id = formBean.getReqdata();
		PlatformMemorandum memorandum = new PlatformMemorandum().selectById(id);
		if(null == memorandum){
			return smartResult(StatusMsg.StatusMsg_1001);
		}
		return smartResult(memorandum);
	}

	/**
	 * 编辑备忘录
	 * @param formBean
	 * @return
	 */
	public SmartResult updateMemorandum(BaseSimpleFormBean<UpdateMemorandumFormBean> formBean){
		if(null == formBean || null == formBean.getReqdata() || 0 == formBean.getReqdata().getId()){
			return emptyDataSmartResult();
		}
		UpdateMemorandumFormBean bean = formBean.getReqdata();
		//获取备忘录
		PlatformMemorandum memorandum = new PlatformMemorandum().selectById(bean.getId());
		if(null == memorandum){
			return smartResult(StatusMsg.StatusMsg_1001);
		}
		BeanUtils.copyProperties(bean, memorandum);
		memorandum.setUpdatetime(new Date());
		if(!memorandum.updateById()){
			return smartResult(StatusMsg.StatusMsg_199);
		}
		return smartResult();
	}

	/**
	 * 完成备忘录
	 * @param formBean
	 * @return
	 */
	public SmartResult completeMemorandum(BaseSimpleFormBean<CompleteMemorandumFormBean> formBean){
		if(null == formBean || null == formBean.getReqdata() || 0 == formBean.getReqdata().getId()
				|| StringUtil.isEmpty(formBean.getReqdata().getSolveScheme())){
			return errorParamSmartResult();
		}
		CompleteMemorandumFormBean bean = formBean.getReqdata();
		//获取备忘录
		PlatformMemorandum memorandum = new PlatformMemorandum().selectById(bean.getId());
		if(null == memorandum){
			return smartResult(StatusMsg.StatusMsg_1001);
		}
		memorandum.setState(2).setSolveScheme(bean.getSolveScheme()).setCompletetime(new Date());
		//修改
		if(!memorandum.updateById()){
			return smartResult(StatusMsg.StatusMsg_199);
		}

		//如果是完成承诺创客10年不收费的，刷新创客或企业的未满10w记录
		if(memorandum.getType() == 4 && memorandum.getState() == 2 ){
			if (memorandum.getCustomerType() == 1 || memorandum.getCustomerType() == 2){
				accountService.statisticsMakerAccountTotalTransaction();
			}
		}
		return smartResult();
	}

}
